package com.base.cn.platform.os.controller.user.member;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.order.goods.OrderDetailsService;
import com.base.cn.platform.os.service.manage.user.member.CusUserMemberBuyRecordService;
import com.base.cn.platform.os.service.manage.user.member.CusUserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户会员接口Controller
 *
 * @author s.li
 * @create 2018-09-05-21:00
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserMemberController extends ApiBaseController {

    @Autowired
    private CusUserMemberBuyRecordService cusUserMemberBuyRecordService;
    @Autowired
    private CusUserMemberService cusUserMemberService;
    @Autowired
    private OrderDetailsService orderDetailsService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 我的会员接口
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/queryMyMember")
    public Map<String,Object> myUserMember(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> record =cusUserMemberBuyRecordService.queryUserBuyMemberRecord(userId);
        Map<String,Object> result = ResultUtil.SUCCESS(record);
        TreeMap<String,String> config = sysConfigService.getSysConfigContext(ConfigName.SALE);
        result.put("sealSwitch",config.get("selaSwitch"));
        return result;
    }

    /**
     * 查询可以购买的会员商品列表
     * @param source 区分请求端
     * @return
     */
    @RequestMapping("/queryUserMemberList")
    public Map<String,Object> queryUserMemberList(@RequestParam(value = "source",required =  false) Integer source){
        Map<String,Object> params = new HashMap<>();
        params.put("status",1);
        params.put("memberType",1);
        params.put("source",source);
        List<Map<String,Object>> userMemberList = cusUserMemberService.queryUserMemberList(params);
        return ResultUtil.SUCCESS(userMemberList);
    }

    /**
     * 查询用户会员开通记录
     * @param userId 用户ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/userMemberBuyRecord")
    public Map<String,Object> userMemberBuyRecord(@RequestParam("userId") BigDecimal userId,
                                                  @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("shopType","MEMBER");
        params.put("statusList","'SUCCESS','REFUND'");
        Map<String, Object> dataMap = orderDetailsService.findOrderDetailsPage(params,currentPage,false,true,false);
        return ResultUtil.SUCCESS(dataMap);
    }

}
